package com.calvin.study.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.calvin.study.mapper.OrderMapper;
import com.calvin.study.service.OrderService;
import com.calvin.study.service.ThreadService;

import cloud.alibaba.study.entity.base.RespCodeEnum;
import cloud.alibaba.study.entity.base.ResponseVo;
import cloud.alibaba.study.entity.order.Order;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.RandomUtil;

@Controller
@RequestMapping("order")
public class OrderController{
	
	@Autowired
	private DiscoveryClient discoveryClient;
	@Value("${server.port}")
	private String serverPort;
	@Value("${spring.application.name}")
	private String springApplicationName;
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * @Title: getNacos   
	 * @Description: 验证我们当前应用是否注册到Nacos服务
	 * @date: 2021年12月17日 上午10:50:16   
	 * @version:
	 * @return          
	 * @throws
	 */
	@GetMapping("getNacos")
	private @ResponseBody ResponseVo getNacos() {
		List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("service-order");
		return ResponseVo.success(serviceInstanceList);
	}
	
	@GetMapping("getOrderList")
	public @ResponseBody ResponseVo getOrderList() {
		try {
			List<Order> orderList = orderMapper.selectList(null);
			return ResponseVo.success(orderList, springApplicationName + ":" + serverPort);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseVo.failure(RespCodeEnum.FAIL, e.getMessage());
		}
	}
	
	@GetMapping("placeOrder")
	public @ResponseBody ResponseVo placeOrder() {
		try {
			Order order = new Order();
			order.setOrderId(new Snowflake().nextId());
			order.setProductId(RandomUtil.randomLong(1, 3));//1-2号商品
			order.setUserId(new Snowflake().nextId());
			order.setCount(RandomUtil.randomInt(1, 21));//随机1-20以内的数量
			order.setUnitPrice(RandomUtil.randomBigDecimal(new BigDecimal(10), new BigDecimal(101)));
			//订单总价，数量*单价
			order.setTotalPrice(order.getUnitPrice().multiply(new BigDecimal(order.getCount())));
			order.setStatus(0);
			ResponseVo resp = orderService.placeOrderV2(order);
			if(resp.getIsOk()) {
				return ResponseVo.success("订单[" + order.getOrderId() + "]，已下单，完成付款尽快发货哦~", springApplicationName + ":" + serverPort);
			}else {
				throw new Exception(resp.getRespMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseVo.failure(RespCodeEnum.FAIL, e.getMessage());
		}
	}
	
	
	@Autowired
	private ThreadService threadService;
	
	@GetMapping("test")
	public @ResponseBody ResponseVo test() {
		try {
			threadService.testThreadPool();
			return ResponseVo.success();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseVo.failure(RespCodeEnum.FAIL, e.getMessage());
		}
	}


}
